package com.netty.pro.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author xuzn
 * @ClassName: NettyByteBuf_01
 * @ProjectName netty-pro
 * @date 2020/3/15/8:46
 */
public class NettyByteBuf_01 {
  public static void main(String[] args) {
    // 创建一个对象,该对象包含一个数组arr,是一个byte[10]
    // netty中的buf不需要进行flip()操作,因为底层维护了readIndex和writeIndex
    // 通过readIndex,writeIndex和capacity将buf分层了三个区域
    // 0-->readIndex:已经读取的区域
    // readIndex-->writeIndex:可读区域
    // writeIndex-->capacity:可写区域
    ByteBuf byteBuf = Unpooled.buffer(10);

    for (int i = 0; i < 10; i++) {
      byteBuf.writeByte(i);
    }
    System.out.println("capacity:" + byteBuf.capacity());
//    for (int i = 0; i < byteBuf.capacity(); i++) {
//      System.out.println("getByte" + i +"=" + byteBuf.getByte(i));
//    }
    for (int i = 0; i < byteBuf.capacity(); i++) {
      System.out.println("readByte" + i + "=" + byteBuf.readByte());
    }
    System.out.println("执行完成~~~");
  }
}
